﻿using CuocChienQueNha.Class.Basic;
using CuocChienQueNha.Class.File;
using CuocChienQueNha.Class.MainMap;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Navigation;

namespace CuocChienQueNha
{
    public partial class MainMap : PhoneApplicationPage
    {
        private ContentManager contentManager;
        private GameTimer timer;
        private SpriteBatch spriteBatch;

        private ButtonGame btn_back, btn_hero, btn_library, btn_Archivement, btn_gold;
        private ButtonGame btn_easy, btn_normal, btn_hard, btn_play;

        private Texture2D bg;

        private string giaodien;
        private Vector2 start, curtouch, dolech;
        private List<TopicMap> maps;
        private List<MiniMap> minimaps;

        public int numberStar { get; set; }
        public int numberGold { get; set; }
        SpriteFont font;

        LevelFile filelevel;
        public static int levelSelected;
        public static int DifficultSelectd;
        public MainMap()
        {
            InitializeComponent();

            // Get the content manager from the application
            contentManager = (Application.Current as App).Content;
            giaodien = "menu";
            numberStar = 20;
            numberGold = 99999;
            levelSelected = 1;
            DifficultSelectd = 1;
            filelevel = new LevelFile();
            // Create a timer for this page
            timer = new GameTimer();
            timer.UpdateInterval = TimeSpan.FromTicks(333333);
            timer.Update += OnUpdate;
            timer.Draw += OnDraw;
            btn_back = new ButtonGame("back", "Images/MainMap/Buton/back", new Vector2(15, 5), new Vector2(88, 45), 1.3333f);
            btn_hero = new ButtonGame("hero", "Images/MainMap/Buton/hero", new Vector2(80, 0), new Vector2(120, 57), 1.3f);
            btn_library = new ButtonGame("library", "Images/MainMap/Buton/Libarary", new Vector2(160, 0), new Vector2(120, 57), 1.3f);
            btn_Archivement = new ButtonGame("Archivement", "Images/MainMap/Buton/Achievement", new Vector2(240, 0), new Vector2(120, 57), 1.3f);
            btn_gold = new ButtonGame("gold", "Images/Mainmap/Buton/Gold", new Vector2(720, 0), new Vector2(120, 57), 1.3f);

            btn_easy = new ButtonGame("1", "Images/Mainmap/Buton/esay1", new Vector2(250, 350), new Vector2(188, 94), 1f);
            btn_normal = new ButtonGame("2", "Images/Mainmap/Buton/Normal2", new Vector2(350, 350), new Vector2(282, 94), 1f);
            btn_hard = new ButtonGame("3", "Images/Mainmap/Buton/Hard1", new Vector2(450, 350), new Vector2(282, 94), 1f);
            btn_play = new ButtonGame("play", "Images/Mainmap/Buton/start", new Vector2(680, 380), new Vector2(190, 78), 1f);

            maps = new List<TopicMap>();
            minimaps = new List<MiniMap>();
            maps.Add(new TopicMap("1", "Images/MainMap/Topic/1", new Vector2(0, 113), new Vector2(248 * 2, 238), 1f));
            maps.Add(new TopicMap("2", "Images/MainMap/Topic/2", new Vector2(350, 113), new Vector2(250 * 2, 238), 1f));
            maps.Add(new TopicMap("3", "Images/MainMap/Topic/3", new Vector2(700, 104), new Vector2(256 * 2, 256), 1f));
            maps.Add(new TopicMap("4", "Images/MainMap/Topic/4", new Vector2(1050, 100), new Vector2(265 * 2, 265), 1f));
            maps.Add(new TopicMap("5", "Images/MainMap/Topic/5", new Vector2(1400, 107), new Vector2(250 * 2, 250), 1f));
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            // Set the sharing mode of the graphics device to turn on XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);

            // TODO: use this.content to load your game content here
            bg = contentManager.Load<Texture2D>("Images/MainMap/background");
            font = contentManager.Load<SpriteFont>("Fonts/Font1");
            btn_back.Load(contentManager);
            btn_hero.Load(contentManager);
            btn_library.Load(contentManager);
            btn_Archivement.Load(contentManager);
            btn_gold.Load(contentManager);
            btn_easy.Load(contentManager);
            btn_normal.Load(contentManager);
            btn_hard.Load(contentManager);
            btn_play.Load(contentManager);
            foreach (TopicMap map in maps)
            {
                map.getElement(filelevel.levels);
                map.Load(contentManager);
            }
            // Start the timer
            timer.Start();

            base.OnNavigatedTo(e);
        }
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Stop the timer
            timer.Stop();

            // Set the sharing mode of the graphics device to turn off XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);
            base.OnNavigatedFrom(e);
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (!giaodien.Equals("menu"))
            {
                levelSelected = 1;
                DifficultSelectd = 1;
                giaodien = "menu";
                dolech = Vector2.Zero;
                e.Cancel = true;
            }
            base.OnBackKeyPress(e);
        }
        private void OnUpdate(object sender, GameTimerEventArgs e)
        {
            // TODO: Add your update logic here

            TouchCollection touchs = TouchPanel.GetState();
            foreach (TouchLocation touch in touchs)
            {
                if (touch.State == TouchLocationState.Pressed)
                {
                    start = touch.Position;
                    curtouch = touch.Position;
                }
                if (touch.State == TouchLocationState.Moved)
                {
                    #region
                    if (giaodien.Equals("menu"))
                    {
                        if (start.Y >= 80)
                        {
                            dolech.X += touch.Position.X - curtouch.X;
                            if (dolech.X >= 0) dolech.X = 0;
                            else if (dolech.X <= -900) dolech.X = -900;
                        }
                        btn_back.isTouch(curtouch);
                        btn_hero.isTouch(curtouch);
                        btn_library.isTouch(curtouch);
                        btn_Archivement.isTouch(curtouch);
                        btn_gold.isTouch(curtouch);
                        foreach (TopicMap topic in maps)
                        {
                            topic.isTouch(curtouch, dolech);
                        }
                    }
                    else if (giaodien.Equals("minimap"))
                    {
                        if (start.Y >= 80)
                        {
                            dolech.X += touch.Position.X - curtouch.X;
                            if (dolech.X >= 0) dolech.X = 0;
                            else if (dolech.X <= -(50 + minimaps.Count * 320) + 800) dolech.X = -(50 + minimaps.Count * 320) + 800;
                        }
                        foreach (MiniMap mini in minimaps)
                        {
                            if (mini.isOpen)
                            {
                                mini.isTouch(curtouch, dolech);
                            }
                        }
                        btn_easy.isTouch(curtouch);
                        btn_normal.isTouch(curtouch, true);
                        btn_hard.isTouch(curtouch, true);
                        btn_play.isTouch(curtouch);
                        btn_back.isTouch(curtouch);
                    }
                    curtouch = touch.Position;
                    #endregion
                }
                if (touch.State == TouchLocationState.Released)
                {
                    curtouch = touch.Position;
                    #region
                    if (Vector2.Distance(start, curtouch) <= 10)
                    {
                        if (giaodien.Equals("menu"))
                        {
                            #region giao dien menu
                            if (btn_back.isTouch(curtouch))
                            {
                                btn_back.isTouched = false;
                                NavigationService.GoBack();
                            }
                            else if (btn_hero.isTouch(curtouch))
                            {
                                btn_hero.isTouched = false;
                                giaodien = "hero";
                            }
                            else if (btn_library.isTouch(curtouch))
                            {
                                btn_library.isTouched = false;
                                giaodien = "library";
                            }
                            else if (btn_Archivement.isTouch(curtouch))
                            {
                                btn_Archivement.isTouched = false;
                                giaodien = "Archivement";
                            }
                            else if (btn_gold.isTouch(curtouch))
                            {
                                btn_gold.isTouched = false;
                                MessageBox.Show("chuc nang dang phat trien");
                            }
                            else
                            {
                                foreach (TopicMap topic in maps)
                                {
                                    if (topic.isTouch(curtouch, dolech))
                                    {
                                        topic.isTouched = false;
                                        if (topic.isOpen)
                                        {
                                            getListMinimap(Convert.ToInt16(topic.name));
                                            giaodien = "minimap";
                                            levelSelected = Convert.ToInt16(minimaps[0].name);
                                            dolech = Vector2.Zero;
                                        }
                                    }
                                }
                            }
                            #endregion
                        }
                        else if (giaodien.Equals("minimap"))
                        {
                            #region giao dien mini
                            if (btn_back.isTouch(curtouch))
                            {
                                btn_back.isTouched = false;
                                giaodien = "menu";
                                dolech = Vector2.Zero;
                            }
                            if (btn_easy.isTouch(curtouch))
                            {
                                btn_easy.isTouched = false;
                                DifficultSelectd = 1;
                            }
                            else if (btn_normal.isTouch(curtouch, true))
                            {
                                btn_normal.isTouched = false;
                                DifficultSelectd = 2;
                            }
                            else if (btn_hard.isTouch(curtouch, true))
                            {
                                btn_hard.isTouched = false;
                                DifficultSelectd = 3;
                            }
                            else if (btn_play.isTouch(curtouch))
                            {
                                btn_play.isTouched = false;
                                NavigationService.Navigate(new Uri("/GamePage.xaml", UriKind.Relative));
                            }

                            foreach (MiniMap mini in minimaps)
                            {
                                if (mini.isOpen)
                                {
                                    if (mini.isTouch(curtouch, dolech))
                                    {
                                        mini.isTouched = false;
                                        levelSelected = Convert.ToInt16(mini.name);
                                    }
                                }
                            }
                            #endregion
                        }
                    }
                    #endregion
                }
            }
        }
        private void OnDraw(object sender, GameTimerEventArgs e)
        {
            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            spriteBatch.Draw(bg, Vector2.Zero, new Rectangle(0, 0, 600, 360), Color.White, 0f, Vector2.Zero, 4 / 3f, SpriteEffects.None, 0);
            if (giaodien.Equals("menu") || giaodien.Equals("minimap"))
            {
                btn_back.Draw(spriteBatch);
                if (giaodien.Equals("menu"))
                {
                    foreach (TopicMap map in maps)
                    {
                        map.Draw(spriteBatch, font, dolech);
                    }
                    btn_hero.Draw(spriteBatch);
                    btn_library.Draw(spriteBatch);
                    btn_Archivement.Draw(spriteBatch);
                    btn_gold.Draw(spriteBatch);
                }
                else
                {
                    foreach (MiniMap map in minimaps)
                    {
                        if (map.image == null) map.Load(contentManager);
                        map.Draw(spriteBatch, dolech);
                    }
                    btn_easy.Draw(spriteBatch);
                    btn_normal.Draw(spriteBatch, true);
                    btn_hard.Draw(spriteBatch, true);
                    btn_play.Draw(spriteBatch);

                    spriteBatch.DrawString(font, "Level: " + levelSelected.ToString(), new Vector2(10, 380), Color.White);
                    spriteBatch.DrawString(font, "Mode: " + (DifficultSelectd == 1 ? "Easy" : (DifficultSelectd == 2 ? "Normal" : "Hard")), new Vector2(10, 410), Color.White);
                }
            }
            spriteBatch.DrawString(font, numberStar.ToString(), new Vector2(560, 11), Color.White);
            spriteBatch.DrawString(font, numberGold.ToString(), new Vector2(650, 11), Color.White);
            spriteBatch.End();
        }
        private void getListMinimap(int topic)
        {
            minimaps.Clear();
            int count = 0;
            foreach (LevelStruct lvstruct in filelevel.levels)
            {
                if (lvstruct.bando == topic)
                {
                    MiniMap newmini = new MiniMap(lvstruct.level.ToString(), "Images/MainMap/miniMap/map" + lvstruct.level, new Vector2(50 + count * 320, 130), new Vector2(284 * 2, 212), 1);
                    newmini.setElement(lvstruct);
                    minimaps.Add(newmini);
                    count++;
                }
            }
        }
    }
}